home *** CD-ROM | disk | FTP | other *** search
/ Dynamic HTML Construction Kit / Dynamic HTML Construction Kit.iso / earthlink / nscomm / java40.jar / netscape / applet / AppletThreadKiller.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-11-03  |  4.8 KB  |  268 lines

  1. package netscape.applet;
  2.  
  3. import java.io.PrintStream;
  4.  
  5. class AppletThreadKiller implements Runnable {
  6.    AppletThreadList list = new AppletThreadList();
  7.    Thread[] threadsSnapshot;
  8.    int threadsSnapshotLen;
  9.    boolean keepRunning = true;
  10.    static ThreadGroup clientThreadGroup;
  11.    static final int SHUTDOWN_TIMEOUT = 30000;
  12.  
  13.    AppletThreadKiller() {
  14.       SecurityManager.enablePrivilege("UniversalThreadGroupAccess");
  15.       clientThreadGroup = new ThreadGroup("ClientThreadGroup");
  16.       Thread var1 = new Thread(this, "Applet Thread Killer");
  17.       var1.start();
  18.    }
  19.  
  20.    synchronized void addAppletThread(AppletThreadList var1) {
  21.       this.list.appendElement(var1);
  22.       this.notify();
  23.    }
  24.  
  25.    synchronized AppletThreadList getAppletThread() {
  26.       AppletThreadList var1 = null;
  27.  
  28.       while(this.keepRunning && this.list.isEmptyList()) {
  29.          try {
  30.             this.notify();
  31.             this.wait();
  32.          } catch (InterruptedException var2) {
  33.          }
  34.       }
  35.  
  36.       if (!this.list.isEmptyList()) {
  37.          var1 = (AppletThreadList)this.list.next;
  38.          ((DoubleLinkedList)var1).remove();
  39.       } else {
  40.          this.notify();
  41.       }
  42.  
  43.       return var1;
  44.    }
  45.  
  46.    synchronized boolean waitForAll() {
  47.       this.notify();
  48.  
  49.       try {
  50.          this.wait((long)(30000 * (MozillaAppletContext.totalApplets + 1)));
  51.       } catch (InterruptedException var1) {
  52.       }
  53.  
  54.       return this.list.isEmptyList();
  55.    }
  56.  
  57.    synchronized boolean shutdown() {
  58.       if (MozillaAppletContext.debug >= 7) {
  59.          System.out.println("##### shutting down all applets");
  60.       }
  61.  
  62.       this.keepRunning = false;
  63.       this.notify();
  64.  
  65.       try {
  66.          this.wait((long)(30000 * (MozillaAppletContext.totalApplets + 1)));
  67.       } catch (InterruptedException var2) {
  68.          if (MozillaAppletContext.debug >= 7) {
  69.             System.out.println("##### applets shutdown interrupted: " + var2);
  70.          }
  71.       }
  72.  
  73.       boolean var1 = clientThreadGroup.activeCount() == 0;
  74.       if (MozillaAppletContext.debug >= 7) {
  75.          System.out.println("##### applets shutdown complete: " + (var1 ? "successful" : "failed"));
  76.       }
  77.  
  78.       return var1;
  79.    }
  80.  
  81.    public void run() {
  82.       Object var1 = null;
  83.       Object var2 = null;
  84.       Object var3 = null;
  85.       Object var4 = null;
  86.       Object var5 = null;
  87.  
  88.       while(this.keepRunning || !this.list.isEmptyList()) {
  89.          try {
  90.             var1 = null;
  91.             var2 = null;
  92.             MozillaAppletContext var93 = null;
  93.             var4 = null;
  94.             var5 = null;
  95.             AppletThreadList var6 = this.getAppletThread();
  96.             if (var6 != null) {
  97.                ThreadGroup var90 = var6.group;
  98.                var6.group = null;
  99.                DerivedAppletFrame var92 = var6.frame;
  100.                var6.frame = null;
  101.                if (var92 != null) {
  102.                   var93 = var92.context;
  103.                }
  104.  
  105.                Task var95 = var6.task;
  106.                var6.task = null;
  107.                Object var98 = null;
  108.                String var97 = var92 != null ? var92.getAppletName() : var95.taskName();
  109.                synchronized(var90){}
  110.  
  111.                try {
  112.                   if (var92 != null) {
  113.                      var92.requestShutdown();
  114.                   } else {
  115.                      var95.requestShutdown();
  116.                   }
  117.  
  118.                   if (!this.keepRunning) {
  119.                      int var9 = var90.activeCount();
  120.                      if (this.threadsSnapshot == null || this.threadsSnapshotLen < var9) {
  121.                         this.threadsSnapshot = new Thread[var9];
  122.                         this.threadsSnapshotLen = var9;
  123.                      }
  124.  
  125.                      var90.enumerate(this.threadsSnapshot);
  126.  
  127.                      for(int var10 = 0; var10 < var9; ++var10) {
  128.                         Thread var11 = this.threadsSnapshot[var10];
  129.                         if (var11.isDaemon()) {
  130.                            var11.stop();
  131.                         }
  132.                      }
  133.  
  134.                      this.threadsSnapshot = null;
  135.                   }
  136.  
  137.                   if (var90.activeCount() > 0) {
  138.                      try {
  139.                         var90.wait(30000L);
  140.                      } catch (InterruptedException var81) {
  141.                         System.err.println("# Interrupted while waiting for applet to die: " + var97);
  142.                      }
  143.                   }
  144.  
  145.                   if (var90.activeCount() > 0) {
  146.                      System.out.println("# Applet " + var97 + " did not shut down within " + 30 + " seconds -- killing it.");
  147.  
  148.                      try {
  149.                         SecurityManager.enablePrivilege("UniversalThreadGroupAccess");
  150.                         var90.stop();
  151.                         var90.wait(5000L);
  152.                      } catch (InterruptedException var79) {
  153.                         System.err.println("# Interrupted while stopping thread group: " + var97);
  154.                      } finally {
  155.                         SecurityManager.revertPrivilege();
  156.                      }
  157.                   }
  158.  
  159.                   try {
  160.                      SecurityManager.enablePrivilege("UniversalThreadGroupAccess");
  161.                      if (var90 instanceof AppletThreadGroup) {
  162.                         ((AppletThreadGroup)var90).destroyItDamnIt();
  163.                      } else if (var90 instanceof TaskThreadGroup) {
  164.                         ((TaskThreadGroup)var90).destroyItDamnIt();
  165.                      } else {
  166.                         var90.destroy();
  167.                      }
  168.                   } catch (Exception var85) {
  169.                      if (MozillaAppletContext.debug >= 7) {
  170.                         System.err.println("# Exception while destroying group: " + var97);
  171.                         ((Throwable)var85).printStackTrace();
  172.                      }
  173.                   } finally {
  174.                      SecurityManager.revertPrivilege();
  175.                   }
  176.  
  177.                   if (var92 != null) {
  178.                      var92.destroy();
  179.                   }
  180.                } catch (Throwable var87) {
  181.                   throw var87;
  182.                }
  183.  
  184.                if (var93 != null) {
  185.                   synchronized(var93){}
  186.  
  187.                   try {
  188.                      if (var93.appletFrames != null && var93.appletFrames.isEmpty()) {
  189.                         if (MozillaAppletContext.debug >= 7) {
  190.                            System.err.println("#   destroyApplet: destroying context for contextID " + var93.contextID);
  191.                         }
  192.  
  193.                         var93.destroy();
  194.                      }
  195.                   } catch (Throwable var84) {
  196.                      throw var84;
  197.                   }
  198.                }
  199.             }
  200.          } catch (Exception var88) {
  201.             System.out.println("Exception occurred while destroying applet: " + ((Throwable)var88).toString());
  202.             if (MozillaAppletContext.debug >= 7) {
  203.                ((Throwable)var88).printStackTrace();
  204.             }
  205.          }
  206.       }
  207.  
  208.       try {
  209.          SecurityManager.enablePrivilege("UniversalThreadGroupAccess");
  210.          clientThreadGroup.stop();
  211.          clientThreadGroup.destroy();
  212.       } catch (Exception var82) {
  213.          if (MozillaAppletContext.debug >= 7) {
  214.             System.err.println("# Exception while destroying group: " + clientThreadGroup);
  215.             ((Throwable)var82).printStackTrace();
  216.          }
  217.       } finally {
  218.          SecurityManager.revertPrivilege();
  219.       }
  220.  
  221.       if (MozillaAppletContext.debug >= 7) {
  222.          System.out.println("# all applets and programs killed");
  223.       }
  224.  
  225.       synchronized(this){}
  226.  
  227.       try {
  228.          this.notify();
  229.       } catch (Throwable var78) {
  230.          throw var78;
  231.       }
  232.  
  233.    }
  234.  
  235.    public String toString() {
  236.       return new String("Applet thread killer! Applets to be disposed: " + this.list);
  237.    }
  238.  
  239.    synchronized void dumpState(PrintStream var1) {
  240.       AppletThreadList var2 = this.list;
  241.       if (var2 != null) {
  242.          boolean var3 = false;
  243.  
  244.          do {
  245.             DerivedAppletFrame var4 = var2.frame;
  246.             Task var5 = var2.task;
  247.             if (!var3 && (var4 != null || var5 != null)) {
  248.                var1.println("# Zombies:");
  249.                var3 = true;
  250.             }
  251.  
  252.             if (var4 != null) {
  253.                try {
  254.                   var4.dumpState(var1, 1);
  255.                } catch (Exception var6) {
  256.                   var1.println("\tApplet: " + var4);
  257.                }
  258.             } else if (var5 != null) {
  259.                var1.println("\tTask: " + var5);
  260.             }
  261.  
  262.             var2 = (AppletThreadList)var2.next;
  263.          } while(var2 != this.list);
  264.  
  265.       }
  266.    }
  267. }
  268.